package org.oschina.api.user.mail.dao;

import java.util.List;
import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.user.mail.model.UserMailType;
import org.oschina.api.user.mail.vo.UserMailTypeVo;
import org.springframework.stereotype.Repository;

@Repository
public class UserMailTypeDAO extends BaseDAO {

	private final String findVo[]={"id","name","text"};
	
	/**
	 * 返回
	 * @return
	 */
	public List<UserMailTypeVo> findVo()
	{
		return getQueryHelper().query_cache(findVo, null, null, null, UserMailTypeVo.class, -1, -1);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public UserMailTypeVo findById(int id)
	{
		UserMailTypeVo cache=findByCache(id);
		if(cache!=null)
			return cache;
		
		return (UserMailTypeVo) getQueryHelper().queryForObject_cache(findVo,new String[]{"id=?"},new Object[]{id},UserMailTypeVo.class);
	}
	private UserMailTypeVo findByCache(int id)
	{
		return (UserMailTypeVo) getCacheHelper().hget(getKey(Constants.CACHE_USER_MAIL_TYPE, id), UserMailTypeVo.class);
	}
	
	/**
	 * 更新
	 * @param userMailType
	 * @return
	 */
	public int update(UserMailType userMailType)
	{
		return (int) getQueryHelper().update_cache(new String[]{"name=?","text=?"}, new String[]{"id=?"}, new Object[]{userMailType.getName(),userMailType.getText(),userMailType.getId()}, UserMailTypeVo.class);
	}
	
}